package io.youngledo.usb.util;

import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextInputDialog;

import java.util.Optional;

/**
 * 对话框工具类 - 统一对话框创建和显示逻辑
 */
public class DialogUtils {

    /// 应用扁平化样式到对话框
    ///
    public static void applyFlatStyle(Dialog<?> dialog) {
        DialogPane dialogPane = dialog.getDialogPane();

        // 加载CSS样式表
        String css = DialogUtils.class.getResource("/styles/application.css").toExternalForm();
        dialogPane.getStylesheets().add(css);

        // 直接设置DialogPane样式，只设置必要的背景和边框
        String dialogPaneStyle =
            "-fx-background-color: white; " +
            "-fx-background-radius: 12; " +
            "-fx-border-radius: 12; " +
            "-fx-border-color: #d2d2d7; " +
            "-fx-border-width: 1;";

        dialogPane.setStyle(dialogPaneStyle);

        // 延迟设置，确保内容区域已创建
        javafx.application.Platform.runLater(() -> {
            // 如果是Alert类型，添加样式类
            if (dialog instanceof Alert) {
                var contentLabel = dialogPane.lookup(".content.label");
                if (contentLabel != null) {
                    contentLabel.getStyleClass().add("dialog-content-white");
                }
            }

            // 如果是TextInputDialog类型，添加样式类
            if (dialog instanceof TextInputDialog) {
                var contentLabel = dialogPane.lookup(".content.label");
                if (contentLabel != null) {
                    contentLabel.getStyleClass().add("dialog-content-white");
                }

                // 查找并设置所有内部容器的样式类
                var allPanes = dialogPane.lookupAll(".pane");
                for (var pane : allPanes) {
                    pane.getStyleClass().add("dialog-pane-white");
                }
            }
        });

        // 如果是Alert类型，移除headerText
        if (dialog instanceof Alert alert) {
            alert.setHeaderText(null);
        }
    }

    /**
     * 显示确认对话框
     */
    public static boolean showConfirmationDialog(String title, String header, String content) {
        Alert confirmAlert = new Alert(Alert.AlertType.CONFIRMATION);
        confirmAlert.setTitle(title);
        confirmAlert.setHeaderText(null); // 不使用headerText
        confirmAlert.setContentText((header != null ? header + "\n\n" : "") + content);
        applyFlatStyle(confirmAlert);

        // Alert的content是Label，添加样式类
        javafx.application.Platform.runLater(() -> {
            var contentLabel = confirmAlert.getDialogPane().lookup(".content.label");
            if (contentLabel != null) {
                contentLabel.getStyleClass().add("dialog-content-white");
            }
        });

        Optional<ButtonType> result = confirmAlert.showAndWait();
        return result.isPresent() && result.get() == ButtonType.OK;
    }

    /**
     * 显示错误对话框（带重试选项）
     */
    public static boolean showErrorWithRetryDialog(String title, String header, String content) {
        Alert errorAlert = new Alert(Alert.AlertType.ERROR);
        errorAlert.setTitle(title);
        errorAlert.setHeaderText(null);
        errorAlert.setContentText((header != null ? header + "\n\n" : "") + content + "\n\n是否要重新尝试？");

        ButtonType retryButton = new ButtonType("重试", ButtonBar.ButtonData.YES);
        ButtonType cancelButton = new ButtonType("取消", ButtonBar.ButtonData.CANCEL_CLOSE);
        errorAlert.getButtonTypes().setAll(retryButton, cancelButton);
        applyFlatStyle(errorAlert);

        javafx.application.Platform.runLater(() -> {
            var contentLabel = errorAlert.getDialogPane().lookup(".content.label");
            if (contentLabel != null) {
                contentLabel.getStyleClass().add("dialog-content-white");
            }
        });

        Optional<ButtonType> choice = errorAlert.showAndWait();
        return choice.isPresent() && choice.get() == retryButton;
    }


    /**
     * 显示文本输入对话框
     */
    public static Optional<String> showTextInputDialog(String title, String header, String content) {
        TextInputDialog dialog = new TextInputDialog();
        dialog.setTitle(title);

        // 如果header为null，就不设置headerText，避免多出一行
        dialog.setHeaderText(header);

        dialog.setContentText(content);
        applyFlatStyle(dialog);

        // 设置对话框最小宽度，避免文本换行
        dialog.getDialogPane().setMinWidth(400);

        return dialog.showAndWait();
    }

}